const shopsModel = require("../model/shopsModel");

// 查询所有服务
module.exports.findAll = async function (searchObj, page) {
  let { current, size } = page;
  let total = await shopsModel.countDocuments(searchObj);
  let totalPage = Math.ceil(total / size);
  let rows = await shopsModel
    .find(searchObj)
    .populate({ path: 'servicesId' })
    .populate({ path: 'goodsId' })
    .populate({ path: 'adminId' })
    .limit(parseInt(size))
    .skip((current - 1) * size);
  return { current, size, total, totalPage, rows };
};

// 增加服务
module.exports.insert = async function (shops) {
  return await shopsModel.create(shops);
};

// 修改服务
module.exports.update = async function (id, shops) {
  return await shopsModel.updateOne({ _id: id }, shops)
}
// 根据ID删除服务
module.exports.delete = async function (id) {
  try {
    return await shopsModel.deleteOne({ _id: id });
  } catch (e) {
    return {};
  }
};

//修改匹配
module.exports.upState = async function (id, status) {
  return await shopsModel.updateOne({ _id: id }, { isAvail: status });
};
